#!/bin/sh

if [ "$0" != "./build-scripts/build-recursor-rpm" ]; then
  echo "Please run me from the root checkout dir"
  exit 1
fi

if [ -z "$VERSION" ]; then
  echo 'Please set $VERSION' >&2
  exit 1
fi

if [ -z "$RELEASE" ];then
  echo 'Please set $RELEASE' >&2
  exit 1
fi

set -e
set -x

# Prepare the build environment
rpmdev-setuptree

# This is somethat ugly...
if [ -f pdns-recursor-${VERSION}.tar.bz2 ]; then
  mv pdns-recursor-${VERSION}.tar.bz2 $HOME/rpmbuild/SOURCES
else
  echo "pdns-recursor-${VERSION}.tar.bz2 not found" >&2
  exit 1
fi

# Some setups need rpmbuild in a 'special' env
RPMBUILD_COMMAND='rpmbuild -bb pdns-recursor.spec'

if [ -f /etc/redhat-release ]; then
  OS="$(cat /etc/redhat-release)"
  case "$OS" in
    Fedora\ *\ 21*)
      exit 1
      ;;
    CentOS\ *\ 6*)
      RPMBUILD_COMMAND="scl enable devtoolset-2 -- ${RPMBUILD_COMMAND}"
      cat > $HOME/rpmbuild/SOURCES/pdns-recursor.init << EOF
#!/bin/bash
#
# pdns-recursor  This shell script controls the PowerDNS Recursor
#
# Author:       Ruben Kerkhof <ruben@rubenkerkhof.com>
#
# chkconfig:    - 49 51
#
# description:  PowerDNS Recursor is a non authoritative/recursing DNS server
# processname:  pdns-recursor
# config: /etc/pdns-recursor/recursor.conf
# pidfile: /var/run/pdns_recursor.pid
#

# source function library
. /etc/rc.d/init.d/functions

RETVAL=0

start() {
	echo -n \$"Starting pdns-recursor: "
	daemon /usr/sbin/pdns_recursor --daemon 2>/dev/null
	RETVAL=\$?
	echo
	[ \$RETVAL -eq 0 ] && touch /var/lock/subsys/pdns-recursor
}

stop() {
	echo -n \$"Stopping pdns-recursor: "
	killproc pdns_recursor
	echo
	[ \$RETVAL -eq 0 ] && rm -f /var/lock/subsys/pdns-recursor && rm -f /var/run/pdns_recursor.controlsocket
}

restart() {
	stop
	start
}

case "\$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|force-reload|reload)
	restart
	;;
  condrestart)
	[ -f /var/lock/subsys/pdns-recursor ] && restart
	;;
  status)
	status pdns_recursor
	RETVAL=\$?
	;;
  *)
	echo \$"Usage: \$0 {start|stop|status|restart|reload|force-reload|condrestart}"
	exit 1
esac

exit \$RETVAL
EOF
      cat > pdns-recursor.spec << EOF
Name: pdns-recursor
Version: ${VERSION}
Release: ${RELEASE}
Summary: Modern, advanced and high performance recursing/non authoritative name server
Group: System Environment/Daemons
License: GPLv2
URL: http://powerdns.com
Source0: ../%{name}-%{version}.tar.bz2
Source1: pdns-recursor.init

Provides: powerdns-recursor = %{version}-%{release}
BuildRequires: boost-devel
BuildRequires: lua-devel

Requires(pre): shadow-utils
Requires(post): /sbin/chkconfig
Requires(preun): /sbin/service, /sbin/chkconfig
Requires(postun): /sbin/service

%description
PowerDNS Recursor is a non authoritative/recursing DNS server. Use this
package if you need a dns cache for your network.

%prep
%setup -q
sed -i -e 's!SYSCONFDIR=/etc/powerdns!SYSCONFDIR=/etc/pdns-recursor!' -e 's!STRIP_BINARIES?=1!STRIP_BINARIES?=0!' $HOME/rpmbuild/BUILD/pdns-recursor-${VERSION}/Makefile.in
sed -i -e 's!OS_SPECIFIC_INSTALL=.*!OS_SPECIFIC_INSTALL=!' $HOME/rpmbuild/BUILD/pdns-recursor-${VERSION}/sysdeps/Linux.inc

%build
%configure
LUA=1 LUA_CPPFLAGS_CONFIG= LUA_LIBS_CONFIG=-llua OPTFLAGS="%{optflags}" LDFLAGS="%{?__global_ldflags}" make %{?_smp_mflags}

%install
%{__rm} -rf %{buildroot}
make install DESTDIR=%{buildroot} CONFIGDIR="%{_sysconfdir}/%{name}"
chmod 0755 %{buildroot}/%{_bindir}/rec_control
chmod 0755 %{buildroot}/%{_sbindir}/pdns_recursor
%{__install} -D -p %{SOURCE1} %{buildroot}%{_initrddir}/pdns-recursor
%{buildroot}/%{_sbindir}/pdns_recursor --config > %{buildroot}%{_sysconfdir}/%{name}/recursor.conf
%{__rm} %{buildroot}%{_sysconfdir}/%{name}/recursor.conf-dist

# add pdns user and group to top of configfile
sed -i '1i\\setuid=pdns-recursor' %{buildroot}%{_sysconfdir}/%{name}/recursor.conf
sed -i '2i\\setgid=pdns-recursor' %{buildroot}%{_sysconfdir}/%{name}/recursor.conf

%pre
getent group pdns-recursor > /dev/null || groupadd -r pdns-recursor
getent passwd pdns-recursor > /dev/null || \
    useradd -r -g pdns-recursor -d / -s /sbin/nologin \
    -c "PowerDNS Recursor user" pdns-recursor
exit 0

%post
chkconfig --add %{name}

%preun
if [ \$1 -eq 0 ]; then
    service %{name} stop >/dev/null 2>&1 || :
    chkconfig --del %{name}
fi

%postun
if [ \$1 -ge 1 ]; then
    service %{name} condrestart >/dev/null 2>&1 || :
fi

%clean
%{__rm} -rf %{buildroot}

%files
%defattr(-,root,root,-)
%{_bindir}/rec_control
%{_sbindir}/pdns_recursor
%{_mandir}/man1/pdns_recursor.1.gz
%{_mandir}/man1/rec_control.1.gz
%{_initrddir}/pdns-recursor
%dir %{_sysconfdir}/%{name}
%config(noreplace) %{_sysconfdir}/%{name}/recursor.conf
%doc COPYING README
EOF
      ;;
    CentOS\ Linux\ *\ 7*)
      cat > pdns-recursor.spec << EOF
%global _hardened_build 1

Name: pdns-recursor
Version: ${VERSION}
Release: ${RELEASE}
Summary: Modern, advanced and high performance recursing/non authoritative name server
Group: System Environment/Daemons
License: GPLv2
URL: http://powerdns.com
Source0: ../%{name}-%{version}.tar.bz2

Provides: powerdns-recursor = %{version}-%{release}
BuildRequires: boost-devel
BuildRequires: lua-devel
BuildRequires: systemd-units
BuildRequires: hostname

Requires(pre): shadow-utils
Requires(post): systemd-sysv
Requires(post): systemd-units
Requires(preun): systemd-units
Requires(postun): systemd-units

%description
PowerDNS Recursor is a non authoritative/recursing DNS server. Use this
package if you need a dns cache for your network.

%prep
%setup -q
sed -i -e 's!SYSCONFDIR=/etc/powerdns!SYSCONFDIR=/etc/pdns-recursor!' -e 's!STRIP_BINARIES?=1!STRIP_BINARIES?=0!' $HOME/rpmbuild/BUILD/pdns-recursor-${VERSION}/Makefile.in
sed -i -e 's!OS_SPECIFIC_INSTALL=.*!OS_SPECIFIC_INSTALL=!' $HOME/rpmbuild/BUILD/pdns-recursor-${VERSION}/sysdeps/Linux.inc

%build
%configure
LUA=1 LUA_CPPFLAGS_CONFIG= LUA_LIBS_CONFIG=-llua OPTFLAGS="%{optflags}" LDFLAGS="%{?__global_ldflags}" make %{?_smp_mflags}

%install
make install DESTDIR=%{buildroot} CONFIGDIR="%{_sysconfdir}/%{name}"
chmod 0755 %{buildroot}/%{_bindir}/rec_control
chmod 0755 %{buildroot}/%{_sbindir}/pdns_recursor
# install systemd unit file
%{__install} -D -p -m 644 contrib/systemd-pdns-recursor.service %{buildroot}%{_unitdir}/%{name}.service

%{buildroot}/%{_sbindir}/pdns_recursor --config > %{buildroot}%{_sysconfdir}/%{name}/recursor.conf
%{__rm} %{buildroot}%{_sysconfdir}/%{name}/recursor.conf-dist

# add pdns user and group to top of configfile
sed -i '1i\\setuid=pdns-recursor' %{buildroot}%{_sysconfdir}/%{name}/recursor.conf
sed -i '2i\\setgid=pdns-recursor' %{buildroot}%{_sysconfdir}/%{name}/recursor.conf

%pre
getent group pdns-recursor > /dev/null || groupadd -r pdns-recursor
getent passwd pdns-recursor > /dev/null || \
    useradd -r -g pdns-recursor -d / -s /sbin/nologin \
    -c "PowerDNS Recursor user" pdns-recursor
exit 0

%post
%systemd_post pdns-recursor.service

%preun
%systemd_preun pdns-recursor.service

%postun
%systemd_postun_with_restart pdns-recursor.service

%triggerun -- pdns-recursor < 3.3-4
# Save the current service runlevel info
# User must manually run systemd-sysv-convert --apply pdns-recursor
# to migrate them to systemd targets
/usr/bin/systemd-sysv-convert --save pdns-recursor >/dev/null 2>&1 ||:

# Run these because the SysV package being removed won't do them
/sbin/chkconfig --del pdns-recursor >/dev/null 2>&1 || :
/bin/systemctl try-restart pdns-recursor.service >/dev/null 2>&1 || :

%files
%{_bindir}/rec_control
%{_sbindir}/pdns_recursor
%{_mandir}/man1/pdns_recursor.1.gz
%{_mandir}/man1/rec_control.1.gz
%{_unitdir}/pdns-recursor.service
%dir %{_sysconfdir}/%{name}
%config(noreplace) %{_sysconfdir}/%{name}/recursor.conf
%doc COPYING README
EOF
      ;;
    *)
      echo "No support for $OS (yet?)"
      exit 1
      ;;
  esac
fi


${RPMBUILD_COMMAND}

mv $HOME/rpmbuild/RPMS/x86_64/pdns-recursor-${VERSION}-${RELEASE}.x86_64.rpm .
[ -f $HOME/rpmbuild/RPMS/x86_64/pdns-recursor-debuginfo-${VERSION}-${RELEASE}.x86_64.rpm ] && mv $HOME/rpmbuild/RPMS/x86_64/pdns-recursor-debuginfo-${VERSION}-${RELEASE}.x86_64.rpm . || true
